FROM centos:6.8

MAINTAINER "Chen Lin" <chenlin@seekasia.com>

WORKDIR /tmp

# Install all required package; Use --nogpgcheck to avoid nokey warning
RUN \
  yum update -y && \

  `########## Install Packages Required To Run SaltStack #########` \
  yum install -y --nogpgcheck gcc wget git zip vixie-cron sendmail && \

  `########## Install SaltStack ##########` \
  curl -L https://bootstrap.saltstack.com -o install_salt.sh && \
  sh install_salt.sh -X

# Deploy SaltStack files
ADD provision/salt/config/minion-localdev /etc/salt/minion
ADD provision/salt/grains/localdev/papi-container /etc/salt/grains
ADD provision/salt/roots /srv/salt
ADD provision/salt/pillar /srv/pillar

# APP_NAMESPACE is used in SaltStack's scripts
ENV APP_NAMESPACE=papi-container

RUN \
  `########## Create Shared Folder ##########` \
  mkdir -p /data && \
  mkdir -p /data/api-root && \
  mkdir -p /data/www && \
  mkdir -p /data/src && \
  mkdir -p /data/log && \
  mkdir -p /data/log/nginx && \
  mkdir -p /data/log/php && \
  mkdir -p /data/log/supervisor && \
  mkdir -p /data/storage && \

  `########## Provision Container Using SaltStack ##########` \
  sed -i 's/local/live/g' /etc/salt/grains && \
  rpm --rebuilddb && \
  output=$(salt-call --local state.highstate -l error) && \
  echo -e "$output" && \
  grep -o 'Failed: 0' <<< $output && \

  `########## Install New Relic daemon ##########` \
  rpm -Uvh http://yum.newrelic.com/pub/newrelic/el5/x86_64/newrelic-repo-5-3.noarch.rpm && \
  yum install -y newrelic-php5 && \

  `########## Cleanup ##########` \
  rm -rf /tmp/* /var/tmp/* && \
  yum clean all && \
  mv /etc/localtime /etc/localtime.bak && \
  ln -s /usr/share/zoneinfo/Asia/Singapore /etc/localtime

# Data Volumes
VOLUME ["/data"]

EXPOSE 80 8008 9001

COPY docker-entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
